﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityLayer;

namespace TestEntity1
{
   public class CompanyEntity:IClassEntity
    {              

        List<Company> Companies;
        XmlHelper<Company> helper;

        public const string ObjectXmlFile = "TestEntity1.xml";

        public string Name
        {
            get { return "Company"; }
        }

        public CompanyEntity()
        {
            helper = new XmlHelper<Company>(CompanyEntity.ObjectXmlFile);
            Companies = helper.Deserialise();
        }

        public System.Windows.Forms.Form Edit(int ID)
        {
            return new EditCompany(ID, Companies);
        }       

        public System.Windows.Forms.Form Add()
        {
            return new EditCompany(null, Companies);
        }

        public List<IObject> Objects
        {
            get {
                List<IObject> result = new List<IObject>();                
                foreach (var h in Companies)
                    result.Add(h);
                return result;
            }
        }

        public void Delete(int ID)
        {
            var h = Companies.Where(i=>i.ID==ID).FirstOrDefault();
            if (h != null)
            {
                Companies.Remove(h);
                helper.Serialise(Companies);
            }
        }
        
    }
}
